import pymysql

class DButils:

    def __init__(self, host, user,password,database,port=3306,charset='utf8'):
        self.host = host
        self.user = user
        self.password = password
        self.database = database
        self.port = port
        self.charset = charset

    def insert(self,name, age, sex, height):
        db = pymysql.connect(
            host=self.host,
            user=self.user,
            password=self.password,
            database=self.database,
            port=self.port,
            charset=self.charset)
        try:
            with db.cursor() as cursor:
                sql = "insert into person values(%s,%s,%s,%s)"
                cursor.execute(sql, (name, age,sex,height))
                db.commit()
                print('插入成功')
        except Exception as e:
            print('插入失败',e)
        finally:
            db.close()

if __name__ == '__main__':
    db = DButils('localhost','root','root','test')
    for i in range(10):
        print(f"\n请输入第 {i + 1} 个人的信息：")
        name = input("姓名: ")
        age = int(input("年龄: "))
        sex = input("性别: ")
        height = float(input("身高: "))
        db.insert(name, age,sex,height)